/*
此插件基于Jquery
插件名：jquery.positionView(定位添加Class插件)
开发者 似懂非懂
版本 1.0
Blog：www.xfei.me
*/
var positionView = {
    init:function(options){
        var t = this;
        t.defualts = {
            elem:undefined,
            speed:2,
            delay:0.2,
        }
        t.options = $.extend({},t.defualts,options);
        t.$elems = t.$elemFun(t.options.elem);
        t.calculateFun();
        t.bindElement();
    },
    //事件绑定
    bindElement:function(){
        var t = this;
        $(window).scroll(function(){
            t.calculateFun();
        });
    },
    //计算执行
    calculateFun:function(){
        var t = this;
        var topV = $(window).scrollTop();
        var wH = $(window).height();
        t.$elems.each(function(index){
            var $sub = t.$elems.eq(index);
            $sub.show();
            var subTopV = $sub.offset().top;
            var startClass = $sub.attr("positionview-startclass");
            var endClass = $sub.attr("positionview-endclass");
            var speed = $sub.attr("positionview-speed");
            if(!speed){
                var speed = t.defualts.speed;
            }
            var delay = $sub.attr("positionview-delay");
            if(!delay){
                var delay = t.defualts.delay;
            }
            if(startClass&&endClass){
                var thisHeight = $sub.height()/3;
                if(subTopV<(topV+wH-thisHeight)){
                    $sub.addClass(startClass);
                    function delayFun(){
                        $sub.addClass(startClass);
                        $sub.removeClass(endClass);
                        $sub.css({
                            "animation-fill-mode":"both",
                            "animation-duration":speed+"s",
                            "-webkit-animation-fill-mode":"both",
                            "-webkit-animation-duration":speed+"s",
                            "animation-delay":delay+"s",
                            "-webkit-animation-delay":delay+"s",
                            "opacity":1,
                        });
                    }
                    setTimeout(function(){
                        delayFun($sub);
                    },0);
                }else{
                    /*$sub.removeClass(startClass);
                    $sub.css({
                        "opacity":0,
                    });*/
                }
            }
        })
        
    },
    //延迟执行
    delayFun:function(){
        var t = this;
    },
    //获取elem元素
    $elemFun:function(name){
        var t = this;
        if(name&&name.length){
            var $elems = $("[positionview-elem='"+name+"']");
        }
        if($elems){
            return $elems;
        }else{
            return null;
        }
    }
}

